package com.stars.services.impl.system;
import com.stars.common.base.ResultMsg;
import com.stars.common.valid.MyException;
import com.stars.dao.mapper.system.SystemUserRelationMapper;
import com.stars.entity.dto.system.SysRoleDTO;
import com.stars.entity.sys.SystemUserRelation;
import com.stars.entity.system.SysRoleEntity;
import com.stars.services.system.SysRoleConfigServices;
import com.stars.dao.mapper.system.SysRoleMapper;
import com.stars.services.system.UserRelationConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.stars.services.impl.BaseConfigServiceImpl;

import java.util.List;

/**
 * @Title: 角色表、角色管理表 业务逻辑
 * @author dj
 * @date 2019-10-14 19:52:12
 * @version V1.0
 *
 */
@Service
public class SysRoleConfigServicesImpl  extends BaseConfigServiceImpl<SysRoleMapper , SysRoleEntity> implements SysRoleConfigServices {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private UserRelationConfigService userRelationConfigService;
    @Autowired
    private SystemUserRelationMapper systemUserRelationMapper;
    @Override
    public ResultMsg add(SysRoleEntity sysRoleEntity, String[] userIds) throws Exception {
        List<SysRoleDTO> byRoleName = sysRoleMapper.findByRoleName(sysRoleEntity.getRoleName());
        if(!byRoleName.isEmpty()){
            throw new MyException("角色名称已经存在");
        }
        //保存角色
        insertSelective(sysRoleEntity);
        //保存角色与用户关系
        userRelationConfigService.addUserRelation(sysRoleEntity.getId(),userIds);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg update(SysRoleEntity sysRoleEntity, String [] userIds,String [] delUserIds) throws Exception {
        List<SysRoleDTO> byRoleNameAndNotId = sysRoleMapper.findByRoleNameAndNotId(sysRoleEntity.getRoleName(), sysRoleEntity.getId());
        if(!byRoleNameAndNotId.isEmpty()){
            throw new MyException("角色名称已经存在");
        }
        updateSelectiveById(sysRoleEntity);
        //删除角色与用户关系
        if(null != delUserIds){
            for(String userId:delUserIds){
                userRelationConfigService.delByRoleId(sysRoleEntity.getId(),userId);
            }
        }
        //保存角色与用户关系
        userRelationConfigService.addUserRelation(sysRoleEntity.getId(),userIds);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg delByRoleIds(String[] ids) throws Exception {
        if(null == ids){
            return ResultMsg.failure("没有选择删除的数据");
        }

        //根据角色id删除关系
        for(String id : ids){
            if(StringUtils.isNotBlank(id)){
                //校验角色是否有资源
                List<SystemUserRelation> byRoleId = systemUserRelationMapper.findByRoleId(id);
                if(!byRoleId.isEmpty()){
                    throw new MyException("角色存在资源，不能删除");
                }
                systemUserRelationMapper.deleteByRoleId(id);
            }
        }
        return deleteByIds(ids);
    }


}